package org.demo.ftp;


import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.Charset;

/**
 * Created by tangwy on 2017/2/19.
 */
public class FTPUtils {

    public static FTPClient create(String userName, String password, String host, int port) throws IOException {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(host, port);
        if (!ftpClient.login(userName, password)) {
            throw new RuntimeException("ftp登录失败");
        }
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            ftpClient.disconnect();
            throw new RuntimeException("远程服务器未响应");
        }
        ftpClient.setCharset(Charset.forName("UTF-8"));
        return ftpClient;
    }

    /**
     * 上传文件
     *
     * @param ftpClient
     * @param file
     * @throws IOException
     */
    public static void uploadFile(FTPClient ftpClient, File file) throws IOException {
        if (ftpClient == null) {
            throw new NullPointerException("ftpClient不能为Null");
        }
        if (file == null) {
            throw new NullPointerException("file不能为Null");
        }
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.setCharset(Charset.forName("UTF-8"));
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                uploadFile(ftpClient, f);
            }
        } else {
            FileInputStream is = new FileInputStream(file);
            boolean result = ftpClient.storeFile(file.getName(), is);//ftpClient.storeFile(new String(file.getName().getBytes("gbk"), "ISO8859-1"), is);
            if (!result) {
                System.out.println(String.format("文件:%s上传失败", file.getName()));
            }
        }
    }

    public static void uploadFile(FTPClient ftpClient, File file, String workDirectory) throws IOException {
        ftpClient.makeDirectory(workDirectory);
        ftpClient.changeWorkingDirectory(workDirectory);
        uploadFile(ftpClient, file);
    }

    public static boolean deleteFile(FTPClient ftpClient, String remoteFile) throws IOException {
        return ftpClient.deleteFile(remoteFile);
    }

    public static boolean removeDirectory(FTPClient ftpClient, String remoteDirectory) throws IOException {
        if(!ftpClient.changeWorkingDirectory(remoteDirectory)){
            System.out.println(String.format("目录%s不存在",remoteDirectory));
        }
        FTPFile[] ftpFiles = ftpClient.listFiles();
        for(FTPFile ftpFile:ftpFiles){
            if(ftpFile.isDirectory()){
                removeDirectory(ftpClient,ftpFile.getName());
            }else {
                deleteFile(ftpClient,ftpFile.getName());
            }
        }
        return ftpClient.removeDirectory(remoteDirectory);
    }
}
